//
//  ThemeDiyScreenShotView.swift
//  HiWidget
//
//  Created by wangws1990 on 2024/10/8.
//

import SwiftUI

struct ThemeDiyScreenShotView: View {
    @State var bindModel: ThemeDiyBindModel
    var model: ThemeDiyWidgetModel? = nil
    let r = UIDevice.current.deviceScale
    var body: some View {
        let w = 173*r
        let h = 355*r
        ZStack{
            Color.clear
            getBgView().frame(width: w,height: h,alignment: .center)
            if let model{
                getRightView().frame(width: w,height: h,alignment: .center).offset(x:38*r,y: -22.5*r).rotationEffect(.degrees(6))
                getLeftView(model).frame(width: w,height: h,alignment: .center).offset(x:-38*r,y: 22.5*r).rotationEffect(.degrees(-6))
            }else{
                getLockView().frame(width: w,height: h,alignment: .center)
            }
        }.frame(width: w,height: h,alignment: .center).clipped()
    }
    @ViewBuilder
    func getBgView()->some View{
        GeometryReader { geo in
            let width = 213*r
            let height = 473*r
            let w = 173*r
            let h = 355*r
            Color.white.frame(width: w,height:h,alignment: .center)
            Image(uiImage: Img(bindModel.bg_lock)).resizable().scaledToFill().frame(width: width,height: height)
            ZStack{
                if #available(iOS 15, *) {
                    LinearGradient(gradient: Gradient(colors: [.white.opacity(0), .white.opacity(0.8)]), startPoint: .topLeading, endPoint: .bottomLeading)
                        .frame(width: w,height:h,alignment: .center).background(.ultraThinMaterial)
                }else{
                    LinearGradient(gradient: Gradient(colors: [.white.opacity(0), .white.opacity(0.8)]), startPoint: .topLeading, endPoint: .bottomTrailing)
                        .frame(width: w,height:h,alignment: .center)
                }
            }.frame(width: w,height:h,alignment: .center)
        }
    }
    
    @ViewBuilder
    func getLeftView(_ model: ThemeDiyWidgetModel)->some View{
        let width = 200*r
        let height = 350*r
        ZStack {
            Image(uiImage: Img("odr_icon_theme_diy_left")).resizable().scaledToFill()
            ZStack{
                ThemeDiyPanelTypeView(panelModel: model, bindModel: $bindModel, fullScreen: true, isEditor: true).frame(width: 124*r,height: 270*r).cornerRadius(15*r, antialiased: true)
            }.frame(width: 124*r,height: 270*r).cornerRadius(15*r, antialiased: true)
        }.frame(width: width,height: height)
    }
    @ViewBuilder
    func getRightView()->some View{
        let width = 200*r
        let height = 350*r
        ZStack {
            Image(uiImage: Img("odr_icon_theme_diy_right")).resizable().scaledToFill()
            ZStack{
                ThemeDiyPanelLockView(model: $bindModel,fullScreen: true,isEditor: false).frame(width: 124*r,height: 270*r).cornerRadius(15*r, antialiased: true)
            }.frame(width: 124*r,height: 270*r).cornerRadius(15*r, antialiased: true)
        }.frame(width: width,height: height)
    }
    
    @ViewBuilder
    func getLockView()->some View{
        let w = 173*r
        let h = 355*r
        ZStack{
            Image("theme_diy_preview_lock").resizable().scaledToFill()
            ThemeDiyPanelLockView(model: $bindModel,fullScreen: true,isEditor: false).frame(width: 133*r,height: 290*r).cornerRadius(14*r, antialiased: true)
        }
        .frame(width: w,height: h).clipped()
    }
}
extension ThemeDiyManage{
    @MainActor
    func saveSnapshot(_ bindModel: ThemeDiyBindModel,model: ThemeDiyWidgetModel? = nil) {
        let r = UIDevice.current.deviceScale
        let width = 173*r
        let height = 355*r
        let sv = ThemeDiyScreenShotView(bindModel: bindModel, model: model)
            .frame(width: width, height: height,alignment: .center)
        if #available(iOS 16.0, *) {
            let renderer = ImageRenderer(content: sv)
            if let image = renderer.uiImage {
                if let data = image.jpegData(compressionQuality: 1) {
                    let filename = "\(Date().timeIntervalSince1970*1000)"
                    let path = FileDirs().getPicDir() + "/" + filename + ".jpg"
                    try? data.write(to: URL(fileURLWithPath: path), options: .atomic)
                    debugPrint(path)
                }
            }
        } else {
            let image =  sv.asImage()
            if let data = image.jpegData(compressionQuality: 1) {
                let filename = "\(Date().timeIntervalSince1970*1000)"
                let path = FileDirs().getPicDir() + "/" + filename + ".jpg"
                try? data.write(to: URL(fileURLWithPath: path), options: .atomic)
                debugPrint(path)
            }
        }
    }
}
